rm(list=ls())

wd <- ".../Replication/"
setwd(wd)

# Load/install packages --
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  foreign, 
  ggplot2, 
  estimatr,
  texreg,
  xtable,
  fastDummies,
  sandwich,
  dplyr,
  janitor, 
  gridExtra,
  gsheet,
  zoo,
  interflex,
  lubridate,
  tidyverse,
  stringi,
  readxl,
  ri2,
  modelsummary,
  ggpubr
)

options(scipen=999)

# Note: given the random component inherent to the randomization tests performed
# by ri(), you may observe small differences in the p-values of the randomization
# tests performed using that function.


# Table 2 --------------------------------------------------------------------

d <- read.csv("Data/baseline database.csv")
lottery <- subset(d, d$group!="Liberado por eleccion")
nowounded <- lottery %>% filter(comment != "herido" | is.na(comment))

# ri setup
declare <- declare_ra(N = nrow(d), m=sum(d$lottery_winner)) 
declare_lotteryonly <- declare_ra(N = nrow(lottery), m=sum(lottery$lottery_winner)) 
declare_nowounded <- declare_ra(N = nrow(nowounded), m=sum(nowounded$lottery_winner)) 


ITT <- list()

ITT[['Detained']] <- with(lottery, difference_in_means(imprisoned_dummy~lottery_winner))
ITT[['Nr of Charges']] <- with(lottery, difference_in_means(imprisoned_nr~lottery_winner))
ITT[['Excluding Wounded']] <- with(nowounded, difference_in_means(imprisoned_dummy~lottery_winner))
ITT[['W Merit']] <- with(d, difference_in_means(imprisoned_dummy~lottery_winner))

ri <- list()

ri[['Detained']] <- summary(conduct_ri(formula = imprisoned_dummy ~ lottery_winner,
                                       declaration = declare_lotteryonly, sharp_hypothesis = 0, assignment = "lottery_winner",
                                       data = lottery, sims = 10000))
ri[['Nr of Charges']] <- summary(conduct_ri(formula = imprisoned_nr ~ lottery_winner,
                                            declaration = declare_lotteryonly, sharp_hypothesis = 0, assignment = "lottery_winner",
                                            data = lottery, sims = 10000))
ri[['Excluding wounded']] <- summary(conduct_ri(formula = imprisoned_dummy ~ lottery_winner,
                                           declaration = declare_nowounded, sharp_hypothesis = 0, assignment = "lottery_winner",
                                           data = nowounded, sims = 10000))
ri[['W Merit']] <- summary(conduct_ri(formula = imprisoned_dummy ~ lottery_winner,
                                      declaration = declare, sharp_hypothesis = 0, assignment = "lottery_winner",
                                      data = d, sims = 10000))

modelsummary(ITT, stars = T, output = "latex")
ri

Table_2<-modelsummary(ITT, stars = T, output = "dataframe")
Table_2$part<-NULL
Table_2[nrow(Table_2)+1,1]<-'Exact p-value'
Table_2[nrow(Table_2),'Detained']<-ri[['Detained']][,"two_tailed_p_value"]
Table_2[nrow(Table_2),'Nr of Charges']<-ri[['Nr of Charges']][,"two_tailed_p_value"]
Table_2[nrow(Table_2),'Excluding Wounded']<-ri[['Excluding wounded']][,"two_tailed_p_value"]
Table_2[nrow(Table_2),'W Merit']<-ri[['W Merit']][,"two_tailed_p_value"]


Table_2
print(xtable(Table_2), file = "Output/Table 2.tex")
